package com.kcloud.commons.authorization.mp.handler.column;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
import com.kcloud.commons.authorization.AuthorizationConfig;

import com.kcloud.commons.authorization.AuthorizationHeaders;
import com.kcloud.commons.authorization.annotation.Authorization;
import com.kcloud.commons.authorization.annotation.AuthorizationGroup;
import com.kcloud.commons.authorization.exception.AuthorizationException;
import com.kcloud.commons.entity.control.Attribute;
import com.kcloud.commons.entity.control.AttributeGroup;
import lombok.Data;
import net.sf.jsqlparser.schema.Column;

import java.util.List;
import java.util.Optional;

/**
 * TODO 查询方法处理器，待ER—>对象—>静态数据的逆向生成结构完成后，继续此部分代码
 * @author songhuiqing
 */
@Data
public class TableColumnSelectHandler implements IAttributeAuthorizationHandler {

    private AuthorizationConfig authorizationConfig;


    @Override
    public List<Column> getAuthColumns(Class<?> authClass, List<Column> columns) {
        //判断当前查询是子集还是主集
        AuthorizationGroup authAnno = authClass.getDeclaredAnnotation(AuthorizationGroup.class);
        AuthorizationHeaders authHeaders = authorizationConfig.getAuthHeaders();
        String configId = authHeaders.getConfigId();
        if(configId == null || "".equalsIgnoreCase(configId)) {
            return columns;
        }
//        TableInfo tableInfo = TableInfoHelper.getTableInfo(authClass);
        List<AttributeGroup> groups = null; //属性集全集
        if(authHeaders.getUserId() != null && !"".equalsIgnoreCase(authHeaders.getUserId())) {
            //用户ID不为空，则筛选用户的权限，否则筛选配置权限
        } else {
            //返回功能配置属性
        }
        Optional<AttributeGroup> attrOptional = groups.stream().filter(g -> authClass.getName().equalsIgnoreCase(g.getGroupCode())).findFirst();
        if(attrOptional.isPresent()) {
            throw new AuthorizationException(String.format("属性集编码与实体对象名称不一致，不区分大小写。实体对象名称为：%s，请确实对应属性集编码是否一致。", authClass.getName()));
        }
        //当前查询对象的属性集合
        List<Attribute> attributes = attrOptional.get().getAttributes();
        //处理对应的列
        return null;
    }
}
